﻿using System;
using Red.Controls.Diagrams.ViewModels;
using Red.Controls.ViewModels;

namespace Red.Controls.Diagrams.Actions.InteropActions
{
    public class AddConnectionAction : InteropActionBase
    {
        #region Data

        readonly ActionConnectionData _connectionData;

        #endregion Data

        #region C-tor

        public AddConnectionAction( DiagramViewModel viewModel, ConnectorViewModel connectorDraggedOut, ConnectorViewModel connectorDraggedOver, ConnectionViewModel newConnection, DataItem parentDataView )
        {
            var inputConnector = connectorDraggedOut.Type == ConnectorType.Input
                ? connectorDraggedOut
                : connectorDraggedOver;

            var outputConnector = connectorDraggedOut.Type == ConnectorType.Output
                ? connectorDraggedOut
                : connectorDraggedOver;

            _parentObject = parentDataView;
            _viewModel = viewModel;
            _connectionData = new ActionConnectionData
            {
                Connection = newConnection,
                InputID = inputConnector.ID,
                OutputID = outputConnector.ID,
                InputOwnerID = inputConnector.ParentNode.ID,
                OutputOwnerID = outputConnector.ParentNode.ID,
                ValueID = Red.GameData.Raw.Utils.FormatGuid( Guid.NewGuid() )
            };
        }

        #endregion C-tor

        #region Public methods

        public void Execute()
        {
            CreateConnection( _connectionData );
        }

        #endregion Public methods
    }
}
